<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="会员卡号" prop="membershipNumber">
                            <el-input v-model="condition.search.membershipNumber" placeholder="会员卡号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="会员姓名" prop="name">
                            <el-input v-model="condition.search.name" placeholder="会员姓名"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="手机号" prop="mobileNo">
                            <el-input v-model="condition.search.mobileNo" placeholder="手机号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="是否管理员" prop="adminFlag">
                            <el-select v-model="condition.search.adminFlag" placeholder="是否管理员">
                                <el-option label="是" :value="true"></el-option>
                                <el-option label="否" :value="false"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="memberCardId"
                label="会员卡ID">
            </el-table-column>
            <el-table-column
                prop="name"
                label="会员姓名">
            </el-table-column>
            <el-table-column
                prop="mobileNo"
                label="手机号">
            </el-table-column>
            <el-table-column
                prop="points"
                width="60"
                label="积分">
            </el-table-column>
            <el-table-column
                prop="balance"
                width="80px"
                label="余额">
            </el-table-column>
            <el-table-column
                prop="avatar"
                label="头像">
                <template v-slot="scope">
                    <el-image style="width: 60px; height: 60px"
                              :src="scope.row.avatar"
                              :preview-src-list="avatarUrls"
                              fit="contain">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="levelName"
                label="等级">
            </el-table-column>
            <el-table-column
                prop="gender"
                :formatter="formatGender"
                label="性别">
            </el-table-column>
            <el-table-column
                prop="birthday"
                label="生日">
            </el-table-column>
            <el-table-column
                prop="flavour"
                label="个人口味">
            </el-table-column>
            <el-table-column
                prop="outerStr"
                label="来源渠道">
            </el-table-column>
            <el-table-column
                prop="joinTime"
                label="入会时间">
            </el-table-column>
            <el-table-column
                prop="adminFlag"
                :formatter="formatAdminFlag"
                label="管理员">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="80px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="onDetail(scope.row)" type="text" size="small">详情</el-button>
                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="会员姓名">
                    <el-input v-model="addOrUpdateForm.name" placeholder="会员姓名"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="addOrUpdateForm.mobileNo" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="addOrUpdateForm.gender" placeholder="性别">
                        <el-option
                            v-for="item in genderOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="来源渠道">
                    <el-input v-model="addOrUpdateForm.outerStr" placeholder="来源渠道"></el-input>
                </el-form-item>
                <el-form-item label="个人口味">
                    <el-input v-model="addOrUpdateForm.flavour" placeholder="个人口味" type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addMember, deleteMember, getMemberByPage, updateMember} from '@/api/api'

export default {
    name: "Member",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 头像url数组
            avatarUrls: [],
            // 性别枚举
            genderOptions: [
                {
                    value: 'MALE',
                    label: '男性'
                },
                {
                    value: 'FEMALE',
                    label: '女性'
                },
            ]
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        memberCardId: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatAdminFlag: function (row, column, cellValue) {
            if (cellValue == undefined) {
                return '';
            }
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatGender: function (row, column, cellValue) {
            if (cellValue == 'MALE') {
                return "男性";
            } else if (cellValue == 'FEMALE') {
                return "女性";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            this.addOrUpdateForm.memberCardId = this.memberCardId;
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addMember(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateMember(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDetail: function (row) {
            this.$router.push({path: '/member/member/detail', query: {id: row.id}});
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteMember({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            this.condition.search.memberCardId = this.memberCardId;
            getMemberByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;

                this.tableData.forEach((item, index, arr) => {
                    this.avatarUrls.push(item.avatar);
                })
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
